package com.example.onlineshop.Activity;

import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;

import com.bumptech.glide.Glide;
import com.example.onlineshop.Domain.CartEntity;
import com.example.onlineshop.Domain.ItemsDomain;
import com.example.onlineshop.R;
import com.example.onlineshop.db.SqliteCarOpenHelper;


import androidx.appcompat.app.AppCompatActivity;

import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.math.BigDecimal;
import java.util.List;

public class DetailActivity extends AppCompatActivity {
private CartEntity object;
private ImageView backBtn, itemImg;
private TextView priceKgTxt, titleTxt, descriptionTxt, ratingTxt;
private RatingBar ratingBar;
private TextView weightTxt, plusBtn, minusBtn, totalTxt,textview;
private int weight = 1;
private RecyclerView.Adapter similarAdapter;
private RecyclerView recyclerViewSimilar;
private ItemsDomain itemsDomain;
private Button cartBtn;



    SqliteCarOpenHelper dbHelper;
    SQLiteDatabase db;

    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_detail);

            findViewById(R.id.cartBtn).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    List<CartEntity> list=dbHelper.QueryByName(object.getTitle());
                    if (list.size()>0){
                        Toast.makeText(DetailActivity.this, "商品已存在",Toast.LENGTH_SHORT).show();
                        return;
                    }
                    long room=dbHelper.insertData(object.getTitle(),object.getImgPath(),object.getPrice()+"",weight,object.getPrice()+"");
                if (room>0){
                    Toast.makeText(DetailActivity.this, "添加成功",Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(DetailActivity.this, "添加失败",Toast.LENGTH_SHORT).show();
                }

                }
            });




            getBundles();
            initView();
            setVariable();
            initSimilarList();
    }



    private void initSimilarList() {
        recyclerViewSimilar=findViewById(R.id.similarView);
        recyclerViewSimilar.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false));

//        similarAdapter = new SimilarAdapter(new MainActivity().getDabta());
//        recyclerViewSimilar.setAdapter(similarAdapter);
    }

    private void setVariable() {
        backBtn.setOnClickListener(v -> DetailActivity.this.finish());

        int drawableResourceId=getResources().getIdentifier(object.getImgPath(),"drawable",DetailActivity.this.getPackageName());

        Glide.with(DetailActivity.this)
                .load(drawableResourceId)
                .into(itemImg);

        priceKgTxt.setText(object.getPrice()+"￥/kg");
        titleTxt.setText(object.getTitle());
        descriptionTxt.setText(object.getDescription());
        ratingTxt.setText("("+object.getRatePrice()+")");
        ratingBar.setRating(Float.parseFloat(object.getRatePrice()));
        totalTxt.setText(weight*Double.parseDouble(object.getPrice()) +"￥");

        plusBtn.setOnClickListener(v -> {
            weight=weight+1;
            weightTxt.setText(weight+" kg");
            BigDecimal bigDecimal=new BigDecimal(weight);  //要用 bigDecimal去做加減乘除
            BigDecimal result3 = bigDecimal.multiply(new BigDecimal(object.getPrice()));
            totalTxt.setText(result3 +"￥");
        });

        minusBtn.setOnClickListener(v -> {
            if (weight>1){
                weight=weight-1;
                weightTxt.setText(weight+" kg");
                BigDecimal bigDecimal=new BigDecimal(weight);  //要用 bigDecimal去做加減乘除
                BigDecimal result3 = bigDecimal.multiply(new BigDecimal(object.getPrice()));
                totalTxt.setText(result3 +"￥");
            }
        });
    }

    private void initView() {
        backBtn=findViewById(R.id.backBtn);
        itemImg = findViewById(R.id.img);
        priceKgTxt = findViewById(R.id.priceKgTxt);
        titleTxt = findViewById(R.id.titleTxt);
        descriptionTxt = findViewById(R.id.deseriptionTxt);
        ratingBar = findViewById(R.id.ratingBar);
        ratingTxt = findViewById(R.id.ratingTxt);
        weightTxt = findViewById(R.id.weightTxt);
        plusBtn = findViewById(R.id.plusBtn);
        minusBtn = findViewById(R.id.minuBtn);
        totalTxt = findViewById(R.id.totalTxt);
        dbHelper = new SqliteCarOpenHelper(this, "cart.db", null, 1);
        db = dbHelper.getWritableDatabase();
    }

    private void getBundles() {
        object= (CartEntity) getIntent().getSerializableExtra("object");
        Log.e("sss",object.getTitle()+"");

    }

}